﻿// SMIL is a community project available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warrenty – Use at your own risk
// View the project and the latest code at http://smil.codeplex.com/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;
using DeepEarth.BingMapsToolkit.Client.Common;
//using DeepEarth.BingMapsToolkit.Client.Common.Entities;
//using DeepEarth.BingMapsToolkit.Client.Common.Commanding;
using Microsoft.Maps.MapControl;
using Microsoft.Maps.MapControl.Core;
using DeepEarth.BingMapsToolkit.Common.Entities;

namespace SMIL.LayerPanel
{
    public static class TileOverlay
    {                

        public static void CreateOverlay(LayerDefinition layerDefinition, TileSource tileSource, MapCore map)
        {                      
            //make sure it hasn't already been added
            foreach (UIElement m in map.Children)
            {
                if ((m is MapTileLayer) && ((MapTileLayer)m).Name == layerDefinition.LayerID)
                    return;
            }
           
            MapTileLayer mtl = new MapTileLayer()
            {
                 Name = layerDefinition.LayerID,
            };            

            mtl.TileSources.Add(tileSource);
            map.Children.Add(mtl);
        }
        
        public static void RemoveOverlay(LayerDefinition layerDefinition, MapCore map)
        {
            //remove the layer by ID            
            foreach (UIElement m in map.Children)
            {
                if ((m is MapTileLayer) && ((MapTileLayer)m).Name == layerDefinition.LayerID)
                {
                    map.Children.Remove(m);
                    return;
                }
            }            
        }

        public static MapTileLayer GetLayerByID(string layerID, MapCore map)
        {
            foreach (UIElement layer in map.Children)
            {
                if (layer is MapTileLayer)
                {
                    var mtl = (MapTileLayer)layer;
                    if (mtl.Name == layerID)
                    {
                        return mtl;
                    }
                }
            }
            return null;
        }
    }
}